<template>
  <div>
    <div class="flex justify-end items-center mt-2">
      <a-button type="primary" class="mr-2" :disabled="store.radioKey" @click="onSign()">签署</a-button>
      <a-button type="primary" class="mr-2" :disabled="store.radioKey" @click="onClickAdd({ brId: brId })">手术预约</a-button>
    </div>
    <div class="absolute top-[-9999px] hidden">
      <a-button ref="printMe" v-print="{ id: 'printMe' }">手术通知单</a-button>
      <PrintView id="printMe" :print-data="printData" />
    </div>
    <template v-if="operationList.length">
      <div v-for="item in operationList" :key="item" class="mt-5 mb-2 border border-solid rounded hover:bg-secondary">
        <div class="h-12 border-b border-solid flex items-center bg-thin">
          <div class="projectName truncate w-[240px] pl-5 font-bold text-left">
            <!-- <div :class="{ 'text-red-500': item.status === 2, 'text-lime-500': item.status === '1' }">{{ item.statusStr }}</div> -->
            <div>
              <span v-if="item.status === '0'" class="text-lime-500">已签署</span>
              <span v-if="item.status === '1'">已安排</span>
              <span v-if="item.status === '2'" class="text-red-500">已作废</span>
              <span v-if="item.status === '3'">待手术</span>
              <span v-if="item.status === '4'">手术中</span>
              <span v-if="item.status === '5'" class="text-lime-500">已手术</span>
              <span v-if="item.status === '6'">已转出</span>
              <span v-if="item.status === '7'">未签署</span>
            </div>
          </div>
          <div class="w-[220px]">预约医生：{{ item.aptmDoctorName || '-' }}</div>
          <div>预约时间：{{ item.aptmTime }}</div>
          <div class="text-right flex-1 mr-6">
            <a-space v-if="item.status !== '2'">
              <a-button v-if="item.status === '7'" type="link" size="small" @click="onClickAdd(item)">修改</a-button>
              <BaseButton v-if="item.status === '7'" confirm-text="确认作废?" type="text" danger size="small" @click="onCancel(item)"> 作废 </BaseButton>
              <BaseButton
                v-if="item.status === '0'"
                :disabled="store.radioKey"
                confirm-text="确认撤销签署?"
                type="text"
                danger
                size="small"
                @click="onSignCancel(item)"
              >
                撤销签署
              </BaseButton>
              <template v-if="item.status === '4' || item.status === '5'">
                <a-button type="link" size="small" @click="onClickRecords(item)">手麻记录</a-button>
              </template>
              <a-button type="link" size="small" :disabled="store.radioKey" @click="onPrint(item)">手术通知单</a-button>
            </a-space>
          </div>
        </div>
        <div class="leading-10 flex items-center justify-between my-2 pl-5">
          <div class="flex-1">
            <div class="flex items-center">
              <LabelValue :width="440" label="手术名称" :value="item.oprName" />
              <LabelValue :width="220" label="手术等级" :value="item.oprLevelName" />
              <LabelValue :width="220" label="部位" :value="item.oprAptmParts" />
              <LabelValue :width="220" label="麻醉方式" :value="item.anaesName" />
              <LabelValue :width="220" label="切口等级" :value="item.cutLevelName" />
            </div>
            <div class="flex items-center">
              <LabelValue :width="220" label="拟手术日期" :value="item.exptOprDate" />
              <LabelValue :width="220" label="手术入路" :value="item.approachName" />
              <LabelValue :width="220" label="手术分类" :value="item.oprTypeName" />
              <LabelValue :width="220" label="手术医生" :value="item.oprDoctorName" />
              <LabelValue :width="220" label="麻醉医生" :value="item.anaesDoctorName" />
              <a-checkbox v-model:checked="item.isEcg" :disabled="true">心电监护</a-checkbox>
              <a-checkbox v-model:checked="item.isRecurrence" :disabled="true">复发</a-checkbox>
            </div>
            <div class="flex items-center">
              <LabelValue :width="660" label="联合手术1" :value="item.jointOprName1" />
              <LabelValue :width="660" label="联合手术2" :value="item.jointOprName2" />
            </div>
            <div class="flex items-center">
              <LabelValue :width="660" label="联合手术3" :value="item.jointOprName3" />
              <LabelValue :width="660" label="联合手术4" :value="item.jointOprName4" />
            </div>
            <div class="flex items-center">
              <LabelValue :width="660" label="联合手术5" :value="item.jointOprName5" />
              <LabelValue :width="660" label="联合手术6" :value="item.jointOprName6" />
            </div>
            <div class="flex items-center">
              <LabelValue :width="660" label="特殊要求" :value="item.specRequirements" />
              <LabelValue :width="660" label="嘱托" :value="item.advMemo" />
            </div>
          </div>
        </div>
      </div>
    </template>
    <template v-else>
      <a-empty />
    </template>
  </div>
  <Edit v-model:open="open" :row-data="rowData" :jzbh="jzbh" :is-out-doctor="isOutDoctor" @success="queryList" />
  <Records v-model:open="openRecords" :row-data="rowDataRecords" />
</template>
<script setup>
import { ref, watch, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import BaseButton from '@/components/BaseButton'
import { post } from '@/utils/request'
import LabelValue from '@/components/LabelValue'
import useStore from '../../store'
import Edit from './Edit'
import Records from './Records'
import PrintView from './PrintView.vue'

const props = defineProps({
  isOutDoctor: {
    //门诊或住院   true 门诊  false 住院
    type: Boolean,
    default: false
  },
  jzbh: {
    type: String,
    default: ''
  },
  brId: {
    type: String,
    default: ''
  }
})
const store = useStore()
const loading = ref(false)
const open = ref(false)
const openRecords = ref(false)
const operationList = ref([])
const rowData = ref({})
const rowDataRecords = ref({})
const printData = ref({})
const printMe = ref()

const onClickAdd = (row = {}) => {
  rowData.value = { ...row }
  open.value = true
}

const onClickRecords = (row) => {
  rowDataRecords.value = { ...row }
  openRecords.value = true
}

const queryList = async () => {
  loading.value = true
  const { code, data, msg } = await post('/oprOperation/opr/getOperationAppointmentList', {
    keyLsh: props.jzbh || store.currentPatient.inplsh,
    isInpAdv: props.isOutDoctor ? '0' : '1'
  })
  loading.value = false
  if (code !== 0) return message.error(msg)
  operationList.value = data
}
//签署
const onSign = async () => {
  // const ids = []
  // operationList.value.forEach((item) => {
  //   if (item.aptmLsh) ids.push(item.aptmLsh)
  // })
  // const { code, msg } = await post('/oprOperation/opr/signedOperation', { aptmLshs: ids, qszt: 1 })
  // if (code !== 0) return message.error(msg || '操作失败')
  // message.success('操作成功')
  // queryList()
  if (props.isOutDoctor) {
    //门诊
    const ids = []
    operationList.value.forEach((item) => {
      if (item.aptmLsh) ids.push(item.aptmLsh)
    })
    const { code, msg } = await post('/oprOperation/opr/signedOperation', { aptmLshs: ids, qszt: 1 })
    if (code !== 0) return message.error(msg || '操作失败')
    message.success('操作成功')
    queryList()
  } else {
    //住院
    const ids = []
    operationList.value.forEach((item) => {
      if (item.inpAdviceId) ids.push(item.inpAdviceId)
    })
    const { code, msg } = await post('/inp/clinicalOrder/signedDoctor', { inpAdvIds: ids, signStatus: 1 })
    if (code !== 0) return message.error(msg || '操作失败')
    message.success('操作成功')
    queryList()
  }
}
//撤销签署
const onSignCancel = async (row) => {
  // const { code, msg } = await post('/oprOperation/opr/signedOperation', { aptmLshs: [row.aptmLsh], qszt: 0 })
  // if (code !== 0) return message.error(msg || '操作失败')
  // message.success('操作成功')
  // queryList()
  if (props.isOutDoctor) {
    //门诊
    const { code, msg } = await post('/oprOperation/opr/signedOperation', { aptmLshs: [row.aptmLsh], qszt: 0 })
    if (code !== 0) return message.error(msg || '操作失败')
    message.success('操作成功')
    queryList()
  } else {
    //住院
    const { code, msg } = await post('/inp/clinicalOrder/signedDoctor', { inpAdvIds: [row.inpAdviceId], signStatus: 0 })
    if (code !== 0) return message.error(msg || '操作失败')
    message.success('操作成功')
    queryList()
  }
}
const onCancel = async (record) => {
  const res = await post('/oprOperation/opr/doUpdateOperationAppoiementStatus', { aptmLsh: record.aptmLsh, status: 2 })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  queryList()
}
//
const onPrint = async (item) => {
  const { code, msg, data } = await post('/oprOperation/oprPrint/surgicalNoticesPrint', { aptmLsh: item.aptmLsh, isInpAdv: props.jzbh ? '0' : '1' })
  if (code !== 0) return message.error(msg)
  if (getSystemConfig('NAVYHIS_MODE') === '1') {
    printData.value = JSON.parse(data)
    setTimeout(() => {
      printMe.value.$el.click()
    }, 1000)
  } else {
    window.openExe?.rpt(data) //调用winform打印
  }
}
onMounted(() => {
  if (store.currentPatient.inplsh || props.jzbh) queryList()
})
watch(
  () => store.currentPatient.inplsh,
  (val) => {
    if (val) queryList()
  }
)
watch(
  () => props.jzbh,
  (val) => {
    if (val) queryList()
  }
)
</script>
<style lang="less" scoped>
.projectName {
  font-size: 20px;
}
</style>
